#include<iostream>
using namespace std;
int arr[100];
int main()
{
	int n;
	cin >> n;
	int sum = 1;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}
	if (arr[1] == 0) {
		arr[1] = 1;
	}
	for (int i = 2; i <= n; i++) {
		if (arr[i] == 0) {
			arr[i] = arr[i - 1] + 1;
		}
		sum += arr[i];
	}
	int i = 2;
	while (i <= n && arr[i] < arr[i + 1]) {
		i++;
	}
	if (i == n) {
		cout << sum << endl;
	}
	else {
		cout << -1 << endl;
	}
	return 0;
}